package databasetest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	
	DefaultTableModel model;
	Connection conn;
	DatabaseTest dt;
	private JScrollPane scrollPane;
	private JTable table;
	JTextField sql;
	private JButton btnNewButton_1;
	/**
	 * Create the frame.
	 */
	public Select(Connection c, DatabaseTest d) {
		super("조회", true, true, true, false);
		this.conn = c;
		this.dt = d;
		setBounds(100, 100, 800, 240);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getSql());
		getContentPane().add(getBtnNewButton_1());

	}
	
	public void setList(String sql){
		try {
			PreparedStatement pstmt = conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector<String> v = new Vector<String>();
			v.add("NO");
			for (int i = 1; i < rsm.getColumnCount()+1; i++) {
				v.add(rsm.getColumnName(i));
			}
			model = new DefaultTableModel(v, 0);
			table.setModel(model);
			int cnt = 1;
			while (rs.next()) {
				Vector d = new Vector<>();
				d.add(cnt);
				for(int i=1; i<=rsm.getColumnCount(); i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
				cnt++;
			}

		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 43, 760, 161);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent arg0) {
					int row = table.getSelectedRow();
					String s = (String)table.getValueAt(row,3);
					String c = (String)table.getValueAt(row,1);
					String e = (String)table.getValueAt(row,2);
					
					if(dt.update ==null){
						dt.openUpdate();
					}
					
					dt.update.setData(s, c, e);
					dt.update.setVisible(true);
					dt.update.toFront();
					
				}
			});
		}
		return table;
	}
	private JTextField getSql() {
		if (sql == null) {
			sql = new JTextField();
			sql.setText("select * from ptbl");
			sql.setBounds(12, 11, 651, 21);
			sql.setColumns(10);
		}
		return sql;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("조회");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setList(sql.getText());
				}
			});
			btnNewButton_1.setBounds(675, 10, 97, 23);
		}
		return btnNewButton_1;
	}
}
